package com.stars.empoly.vmodel

import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.stars.empoly.activity.ArticleDetailActivity
import com.stars.empoly.activity.ChangePhoneActivity
import com.stars.empoly.activity.DestructionActivity
import com.stars.empoly.activity.OneLoginActivity
import com.stars.empoly.jupsh.componnent.JpushUtils
import com.stars.empoly.model.User
import com.stars.empoly.update.bean.UpdateBean
import com.stars.empoly.utils.net.NetEngine
import com.tencent.mmkv.MMKV
import com.trailer.base.base.BRepository
import com.trailer.base.base.BViewModel
import com.trailer.base.utils.B
import com.trailer.base.utils.UiSwitch
import kotlinx.coroutines.launch


class SettingVModel(application: Application) : BViewModel<BRepository>(application) {


    var bean= MutableLiveData<UpdateBean>()

    var phone=MutableLiveData<String>()


    //更换手机号
    fun toChangePhone(){
        UiSwitch.single(repository().context, ChangePhoneActivity::class.java)

    }

    //注销手机号
    fun toDestruction(){

        UiSwitch.single(repository().context, DestructionActivity::class.java)

    }

    fun toAgreement(type:Int){
        if(type==1){//服务协议

            UiSwitch.bundle(repository().context,
                    ArticleDetailActivity::class.java, B().putString("type","1").ok())
        }else{//隐私协议
            UiSwitch.bundle(repository().context,
                    ArticleDetailActivity::class.java, B().putString("type","2").ok())

        }

    }

    fun toDestruct(){
        viewModelScope.launch {
            var it=repository().lreq { NetEngine.service.memberLogout() }
            if(it.ok(true)){
//                JpushUtils.setClearAlias(repository().context, User.get()!!.id)
                MMKV.defaultMMKV().putString("token", "")
                UiSwitch.single_new_task_with_clear_task(repository().context, OneLoginActivity::class.java)
            }
        }
    }


    fun getRate(){
        viewModelScope.launch {
            var it=repository().lreq { NetEngine.service.configList() }
            if(it.ok()){
                phone.value=it.data?.platformPhone
            }
        }
    }




    fun getAppConfig(){
        viewModelScope.launch {
            var it=repository().lreq { NetEngine.service.payOrder("0") }
            if(it.ok()){
                bean.value=it.data
            }

        }
    }



}



